package banner

import (
	"context"
	"strconv"

	"mall/common/globalkey"
	"mall/common/utils/pointy"
	"mall/service/backend/api/internal/logic/system/util"
	"mall/service/backend/api/internal/logic/utils"
	"mall/service/backend/api/internal/logic/utils/parse"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/i18n"
	"mall/service/backend/common/utils/miniapp"
	"mall/service/backend/rpc/backendclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetBannerListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetBannerListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBannerListLogic {
	return &GetBannerListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetBannerListLogic) GetBannerList(req *types.BannerListReq) (resp *types.BannerListResp, err error) {
	page, pageSize := parse.ParsePageInfo(
		req.Current,
		req.PageSize,
		l.svcCtx.Config.Query.DefaultAmount,
		l.svcCtx.Config.Query.MaxAmount,
	)

	orderBys := util.ParseSorterNew(req.Sorter)
	filterBys := util.ParseFilterNew(req.Filter, req.Tab)

	var ids *string
	if req.Id != nil && *req.Id > 0 {
		ids = pointy.GetPointer(strconv.FormatInt(int64(*req.Id), 10))
	}

	data, err := l.svcCtx.BackendRpc.BackendGetBannerList(l.ctx, &backendclient.BannerListReq{
		Page:      page,
		PageSize:  pageSize,
		Ids:       ids,
		Type:      req.Type,
		Title:     req.Title,
		Status:    req.Status,
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
		OrderBy:   orderBys,
		FilterBy:  filterBys,
	})
	if err != nil {
		return
	}

	resp = &types.BannerListResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: types.BannerListInfo{
			BaseListInfo: types.BaseListInfo{
				Current:  page,
				PageSize: pageSize,
				Total:    data.GetTotal(),
			},
			StatusStatisticMap: data.StatusStatisticMap,
		},
	}

	for _, v := range data.List {
		var media []*types.MediaInfo
		if len(v.Media) > 0 {
			for _, m := range v.Media {
				var url *string
				if m.Url != nil {
					url = m.Url
				} else if m.Uri != nil {
					url = m.Uri
				}
				media = append(media, &types.MediaInfo{
					Uid:       m.Uid,
					Id:        m.Id,
					Thumbnail: m.Thumbnail,
					Order:     m.Order,
					Url:       url,
					Name:      m.Name,
					MimeType:  m.MimeType,
					AltText:   m.AltText,
					MetaData:  m.MetaData,
				})
			}
		}

		option := utils.GetOptionByStatus(v.Status)
		var linkUrl *string
		var linkUrlInvalid bool
		if v.LinkType != nil && *v.LinkType == uint32(globalkey.SettingBannerLinkTypeAbsolute) { // H5 绝对路径
			linkUrl = v.LinkUrl
		} else {
			url := miniapp.GetLinkUrl(v.LinkType, v.AdType, v.AdId, v.LinkUrl)
			if len(url) > 0 {
				linkUrl = pointy.GetPointer(url)
			} else if v.LinkUrl != nil && len(*v.LinkUrl) > 0 {
				linkUrl = v.LinkUrl
				linkUrlInvalid = true
			}
		}
		resp.Data.List = append(resp.Data.List, &types.BannerInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:         v.Id,
				CreatedAt:  v.CreatedAt,
				UpdatedAt:  v.UpdatedAt,
				DeletedAt:  v.DeletedAt,
				CanAccept:  option.CanAccept,
				CanReject:  option.CanReject,
				CanRemove:  option.CanRemove,
				CanRestore: option.CanRestore,
			},
			PicUrl:         v.PicUrl,
			Type:           v.Type,
			Title:          v.Title,
			LinkType:       v.LinkType,
			LinkUrl:        linkUrl,
			LinkUrlInvalid: linkUrlInvalid,
			AdType:         v.AdType,
			AdId:           v.AdId,
			Status:         v.Status,
			Sort:           v.Sort,
			StartedAt:      v.StartedAt,
			EndedAt:        v.EndedAt,
			Media:          media,
		})
	}

	return resp, nil
}
